﻿
using SimpleConnection_UDP_WIN.utils;
using SimpleConnection_UDP_WIN.utils.Json;
using SimpleConnection_UDP_WIN.utils.UDP;
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Windows.Forms;
using WindowsImageCompressionApp.utils;
using static SimpleConnection_UDP_WIN.utils.Json.ScCmdJson;

namespace SimpleConnection_UDP_WIN.ui
{
    public partial class JsonEditForm : Form
    {
        public JsonEditForm()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            textBox2.Text = "255.255.255.255";
            textBox3.Text = "8870";
            textBox4.Text = "广播类型信息";
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            editBox.Text = "";
            editBox.ReadOnly = false;

            ViewUtil.setEditBoxEmpty(textBox2, textBox3, textBox4);
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(editBox.Text);
        }

        private void addCursorPosition(string textToInsert)
        {
            int cursorPosition = editBox.SelectionStart;
            editBox.Text = editBox.Text.Insert(cursorPosition, textToInsert);
            editBox.SelectionStart = cursorPosition + textToInsert.Length / 2;

        }
        private void addCursorPosition(string textToInsert, int forwardPos)
        {
            int cursorPosition = editBox.SelectionStart;
            editBox.Text = editBox.Text.Insert(cursorPosition, textToInsert);
            editBox.SelectionStart = cursorPosition + textToInsert.Length - forwardPos;

        }

        private void menuItem5_Click(object sender, EventArgs e)
        {
            addCursorPosition("<key></key>");
        }

        private void menuItem6_Click(object sender, EventArgs e)
        {
            addCursorPosition("<string></string>");
        }

        private void menuItem7_Click(object sender, EventArgs e)
        {
            addCursorPosition("<num></num>");
        }

        private void menuItem8_Click(object sender, EventArgs e)
        {
            addCursorPosition($"<pw>{RegistryUtils.getString("password", "123456")}</pw>", forwardPos: 5);
        }

        private void menuItem9_Click(object sender, EventArgs e)
        {
            editBox.SelectionStart = 0;
            addCursorPosition("://");
            editBox.SelectionStart = 0;
        }

        private void menuItem13_Click(object sender, EventArgs e)
        {

            if (ViewUtil.IsFormTextBoxValid(groupBox1.Controls) && ViewUtil.IsFormTextBoxValid(Controls))
            {
                new UdpTool(textBox2.Text, int.Parse(textBox3.Text)).SendMessage(editBox.Text);
            }
            else
            {
                statusLabel2.Text = "各编辑框均不能为空";
            }

        }

        private void menuItem15_Click(object sender, EventArgs e)
        {
            editBox.SelectionStart = editBox.TextLength;
        }

        private void menuItem14_Click(object sender, EventArgs e)
        {
            editBox.SelectionStart = 0;
        }

        private void JsonEditForm_Load(object sender, EventArgs e)
        {
            //添加额外的 样式，用于json
            editBox.AddStyle(null);
        }

        private void menuItem10_Click(object sender, EventArgs e)
        {
            if (ViewUtil.IsFormTextBoxValid(groupBox1.Controls) && ViewUtil.IsFormTextBoxValid(Controls))
            {

                //转换 json并输出到编辑框，编辑框 更改为只读 ，

                ScCmdJson scCmdJson = new ScCmdJson();

                scCmdJson.cmd = new List<hobby>();
                //组装 一个json对象
                hobby h = new hobby();
                h.name = textBox1.Text;
                h.ip = textBox2.Text;
                h.port = int.Parse(textBox3.Text);
                h.portString = textBox3.Text;
                h.note = textBox4.Text;
                h.id = new Random().Next(10000);
                h.content = editBox.Text;
                //添加进 列表
                scCmdJson.cmd.Add(h);
                //序列化开始
                var options = new JsonSerializerOptions { WriteIndented = true };
                string jsonStr = JsonSerializer.Serialize(scCmdJson,options);
                //进行前后截断
             //   jsonStr = jsonStr.Substring(7);
             //   jsonStr = jsonStr.Substring(0, jsonStr.Length - 1);
                editBox.Text = jsonStr;

                editBox.ReadOnly = true;
                statusLabel2.Text = "转换完成，请复制完之后，开始新命令";

            }
            else
            {
                statusLabel2.Text = "各编辑框均不能为空";
            }



        }

        private void menuItem17_Click(object sender, EventArgs e)
        {
            DBUtil.openLink(EDIT_WEB_LINK);
        }

        private const string EDIT_WEB_LINK = "https://dabai2017.gitee.io/simple-connection-udp-win";
    }
}
